"use strict";

exports.__esModule = true;

var _find = require("babel-runtime/core-js/array/find");

var _find2 = _interopRequireDefault(_find);

exports.default = createHandlersMiddleware;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function createHandlersMiddleware() {
  var handlers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

  return function (middlewareAPI) {
    return function (next) {
      return function (action) {
        var _action$meta = action.meta,
            meta = _action$meta === undefined ? {} : _action$meta;

        var actionHandler = (0, _find2.default)(handlers.concat(meta.handlers || []), function (h) {
          return h.type === action.type;
        });
        var beforeHandler = actionHandler && actionHandler.beforeHandler;
        var afterHandler = actionHandler && actionHandler.afterHandler;

        if (beforeHandler) {
          beforeHandler(middlewareAPI, action);
        }

        var result = next(action);

        if (afterHandler) {
          afterHandler(middlewareAPI, action);
        }

        return result;
      };
    };
  };
}